package com.shine.minepressure.view.pop;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BottomPopupView;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lxj.xpopup.util.XPopupUtils;
import com.shine.minepressure.R;
import com.shine.minepressure.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

public class RealTimePopup extends BottomPopupView implements View.OnClickListener {
    OnConfirmListener confirmListener;
    OnCancelListener cancelListener;
    List<String> mAreaMessageList = new ArrayList<>();
    List<String> mSensorMessageList = new ArrayList<>();
    private String mArea, mSensorType, mSensorTypeName;
    private TextView areaMessageText, sensorMessageText, areaSensorMessageText;
    private Integer mAreaPosition, mSensorTypePosition, mAreaSensorMessagePosition;

    public interface OnPopClickListener {
        void onPopClick(View view, Integer areaPosition, Integer sensorTypePosition);

        void onConfirmClick(Integer areaPosition, Integer sensorTypePosition, Integer areaSensorMessagePosition, String mArea, String mSensorType, String mSensorTypeName);
    }

    private OnPopClickListener onPopClickListener;

    public RealTimePopup(@NonNull Context context, List<String> areaMessageList, List<String> sensorMessageList,
                         String areaId, String sensorType, String sensorTypeName,
                         Integer areaPosition, Integer sensorTypePosition, Integer areaSensorMessagePosition) {
        super(context);
        mAreaMessageList.addAll(areaMessageList);
        mSensorMessageList.addAll(sensorMessageList);

        mArea = areaId;
        mSensorType = sensorType;
        mSensorTypeName = sensorTypeName;
        mAreaPosition = areaPosition;
        mSensorTypePosition = sensorTypePosition;
        mAreaSensorMessagePosition = areaSensorMessagePosition;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.popup_real_time;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        ImageView cancel = (ImageView) findViewById(R.id.cancel);
        TextView confirm = (TextView) findViewById(R.id.confirm);

        areaMessageText = (TextView) findViewById(R.id.areaMessageText);
        sensorMessageText = (TextView) findViewById(R.id.sensorMessageText);
        areaSensorMessageText = (TextView) findViewById(R.id.areaSensorMessageText);

        if (!TextUtils.isEmpty(mArea))
            areaMessageText.setText(mArea);

        if (!TextUtils.isEmpty(mSensorType))
            sensorMessageText.setText(mSensorType);

        if (!TextUtils.isEmpty(mSensorTypeName))
            areaSensorMessageText.setText(mSensorTypeName);

        confirm.setOnClickListener(this);
        cancel.setOnClickListener(this);

        findViewById(R.id.areaMessageLayout).setOnClickListener(this);
        findViewById(R.id.sensorMessageLayout).setOnClickListener(this);
        findViewById(R.id.areaSensorMessageLayout).setOnClickListener(this);

    }

    public RealTimePopup setListener(OnConfirmListener confirmListener, OnCancelListener onCancelListener, OnPopClickListener onPopClickListener) {
        this.confirmListener = confirmListener;
        this.cancelListener = onCancelListener;
        this.onPopClickListener = onPopClickListener;
        return this;
    }

    private void setAreaMessage(List<String> areaMessageList) {
        CommonListBottomPopup commonListBottomPopup = new CommonListBottomPopup(getContext(), areaMessageList);
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isDestroyOnDismiss(true)
                .maxHeight(200)
                .asCustom(commonListBottomPopup)/*.enableDrag(false)*/
                .show();
        commonListBottomPopup.setListener(new CommonListBottomPopup.OnPopItemClickListener() {
            @Override
            public void onPopItemClick(View view, int position) {
                mAreaPosition = position;
                areaMessageText.setText(areaMessageList.get(position));
                commonListBottomPopup.dismiss();
            }
        });
    }

    private void setSensorMessage(List<String> sensorMessageList) {
        CommonListBottomPopup commonListBottomPopup1 = new CommonListBottomPopup(getContext(), sensorMessageList);
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isDestroyOnDismiss(true)
                .maxHeight(200)
                .asCustom(commonListBottomPopup1)/*.enableDrag(false)*/
                .show();
        commonListBottomPopup1.setListener(new CommonListBottomPopup.OnPopItemClickListener() {
            @Override
            public void onPopItemClick(View view, int position) {
                mSensorTypePosition = position;
                sensorMessageText.setText(sensorMessageList.get(position));
                commonListBottomPopup1.dismiss();
            }
        });
    }

    public void setAreaSensorMessage(List<String> areaSensorMessageList) {
        CommonListBottomPopup commonListBottomPopup2 = new CommonListBottomPopup(getContext(), areaSensorMessageList);
        new XPopup.Builder(getContext())
                .moveUpToKeyboard(false) //如果不加这个，评论弹窗会移动到软键盘上面
                .enableDrag(true)
                .isDestroyOnDismiss(true)
                .maxHeight(200)
                .asCustom(commonListBottomPopup2)/*.enableDrag(false)*/
                .show();
        commonListBottomPopup2.setListener(new CommonListBottomPopup.OnPopItemClickListener() {
            @Override
            public void onPopItemClick(View view, int position) {
                mAreaSensorMessagePosition = position;
                areaSensorMessageText.setText(areaSensorMessageList.get(position));
                commonListBottomPopup2.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm:
                if (confirmListener != null) confirmListener.onConfirm();

                if (areaMessageText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择测区列表");
                    return;
                }
                if (sensorMessageText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择传感器信息");
                    return;
                }
                if (areaSensorMessageText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择指定测区传感器");
                    return;
                }
                onPopClickListener.onConfirmClick(mAreaPosition, mSensorTypePosition, mAreaSensorMessagePosition,
                        areaMessageText.getText().toString(), sensorMessageText.getText().toString(), areaSensorMessageText.getText().toString());
                dismiss();
                break;
            case R.id.cancel:
                if (cancelListener != null) cancelListener.onCancel();
                dismiss();
                break;
            case R.id.areaMessageLayout:
                setAreaMessage(mAreaMessageList);
                break;
            case R.id.sensorMessageLayout:
                setSensorMessage(mSensorMessageList);
                break;
            case R.id.areaSensorMessageLayout:
                if (areaMessageText.getText().toString().isEmpty()) {
                    ToastUtils.showShort("请选择测区列表");
                    return;
                }
                onPopClickListener.onPopClick(v, mAreaPosition, mSensorTypePosition);
                break;
            default:
                break;
        }
    }

    //完全可见执行
    @Override
    protected void onShow() {
        super.onShow();
    }

    //完全消失执行
    @Override
    protected void onDismiss() {
    }

    @Override
    protected int getMaxHeight() {
//        return XPopupUtils.getWindowHeight(getContext());
        return (int) (XPopupUtils.getWindowHeight(getContext()) * .9f);
    }

}
